package net.zoneland.knowledge.controller;


import lombok.extern.slf4j.Slf4j;
import net.zoneland.knowledge.constant.Constants;
import net.zoneland.knowledge.model.LoginUserPojo;
import net.zoneland.knowledge.model.PopupPrompt;
import net.zoneland.knowledge.model.PopupPromptLog;
import net.zoneland.knowledge.model.ResponseResult;
import net.zoneland.knowledge.service.PopupPromptService;
import net.zoneland.knowledge.utils.LoginUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 新功能提醒控制层
 *
 * @author 王志伟
 * @date 2023/5/23
 */
@Slf4j
@RestController()
@RequestMapping("/popupPrompt")
public class PopupPromptController extends AbstractBaseController {

    /**
     * 新功能提醒业务接口.
     */
    @Autowired
    private PopupPromptService popupPromptService;

    /**
     * 获取新功能提醒.
     *
     * @return 结果集
     */
    @PostMapping("/getPopupPrompt")
    public ResponseResult getPopupPrompt(final HttpServletRequest request) {
        final ResponseResult res = new ResponseResult();
        final LoginUserPojo user = LoginUtils.getCurrentUserByToken(request.getHeader(Constants.TOKEN_NAME));
        if (Objects.isNull(user)) {
            return res;
        }
        final List<PopupPromptLog> logList = popupPromptService.findPopupPromptLogByUserId(user.getUserID());
        final List<PopupPrompt> popupId = popupPromptService.findPopupPromptId();

        //如果loglist和popupId都不为空就会去获取日志表中不存在的新功能id
        //如果popupId为空就说明新功能表没有数据
        if (CollectionUtils.isNotEmpty(popupId)) {
            if (CollectionUtils.isNotEmpty(logList)) {
                handleLogListAndPopupId(res, user, logList, popupId);
            } else {
                handleEmptyLogList(res, user, popupId);
            }
        }
        return res;
    }

    /**
     * 获取日志表中不存在的新功能id
     * @param res 统一返回结果信息
     * @param user 用户信息
     * @param logList 日志表内容
     * @param popupId 功能提醒表id
     */
    private void handleLogListAndPopupId(final ResponseResult res,
                                         final LoginUserPojo user,
                                         final List<PopupPromptLog> logList,
                                         final List<PopupPrompt> popupId) {
        final List<PopupPrompt> differentIds = popupId.stream()
                .filter(p -> logList.stream().noneMatch(log -> log.getNewFunctionId().equals(p.getId())))
                .collect(Collectors.toList());

        if (!differentIds.isEmpty()) {
            final String id = differentIds.get(0).getId();
            final PopupPrompt contentById = popupPromptService.findContentById(id);
            getContentAndId(res, user, contentById);
        }
    }


    /**
     *获取最新的内容
     * @param res 统一结果返回信息
     * @param user 当前用户信息
     * @param popupId 多个id集合
     */
    private void handleEmptyLogList(final ResponseResult res,
                                    final LoginUserPojo user,
                                    final List<PopupPrompt> popupId) {
        //获取到最新一条数据，因为popupId已经做了排序，所以第一条就是最新的一条
        final String id = popupId.get(0).getId();
        final PopupPrompt contentByDateTime = popupPromptService.findContentById(id);
        getContentAndId(res, user, contentByDateTime);
    }

    /**
     * 获取具体内容和功能提醒id
     * @param res 统一结果返回信息
     * @param user 用户信息
     * @param popupPrompt 内容和功能id
     */
    private void getContentAndId(final ResponseResult res,
                                 final LoginUserPojo user,
                                 final PopupPrompt popupPrompt) {
        if (!Objects.isNull(popupPrompt)) {
            popupPromptService.addPopupPromptLog(user.getUserID(), user.getUserName(), popupPrompt.getId());
            res.setData(popupPrompt.getContent());
        }
    }
}


